﻿using KingsSharp.Common.Data;
using KingsSharp.Core;
using KingsSharp.Core.Data;
using CRM.Admin.Contracts.Contract.Sys;
using CRM.Admin.Contracts.Model.Sys;
using System;
using System.Linq;

namespace CRM.Admin.Services.Service.Sys
{
    public partial class MenuService: ServiceBase,IMenuService
    {
        /// <summary>
        /// 菜单目录
        /// </summary>
        private readonly IRepository<MenuCatalog, Guid> _menucatologRepository;
        /// <summary>
        /// 菜单
        /// </summary>
        private readonly IRepository<MenuNode, Guid> _menunodeRepository;
        /// <summary>
        /// 菜单权限组
        /// </summary>
        private readonly IRepository<MenuNodeAction, int> _menunodeactionRepository;
        public MenuService(IRepository<MenuCatalog, Guid> menucatologRepository,
            IRepository<MenuNode, Guid> menunodeRepository,
            IRepository<MenuNodeAction, int> menunodeactionRepository)
            : base(menucatologRepository.UnitOfWork)
        {
            _menucatologRepository = menucatologRepository;
            _menunodeRepository = menunodeRepository;
            _menunodeactionRepository = menunodeactionRepository;
        }

        public OperationResult InitEco(MenuCatalog catalogdto)
        {
            OperationResult result = new OperationResult(OperationResultType.NoChanged,"数据已初始化、无需重复初始化");
            var catalog = _menucatologRepository.Entities.Where(
                entity => entity.Name == catalogdto.Name && entity.Order == catalogdto.Order
                    && entity.IsDeleted == false && entity.Type == catalogdto.Type
                ).FirstOrDefault();
          
            if (catalog == null)
            {
                _menucatologRepository.Insert(catalogdto);
                result.ResultType = OperationResultType.Success;
                result.Message = "数据库初始化成功";
            }
            return result;
        }
    }
}
